//
//  DetailViewController.m
//  ContactViewerIos
//
//  Created by ANDY SELVIG on 3/7/12.
//  Copyright (c) 2012 Tiny Mission. All rights reserved.
//

#import "ContactActionViewController.h"
#import "ContactList.h"
#import "DetailViewController.h"

@interface DetailViewController ()
@property (strong, nonatomic) UIPopoverController *masterPopoverController;
- (void)configureView;
@end

@implementation DetailViewController

NSString* CONFIRMATION_POPUP_TEXT = @"Confirm Deletion of";
NSString* CANCEL_TEXT = @"Cancel";
NSString* CONFIRM_TEXT = @"Confrim";
NSInteger CANCEL_BUTTON_INDEX = 0;
NSInteger CONFIRM_BUTTON_INDEX = 1;

@synthesize contactActionViewController;
@synthesize detailItem = _detailItem;
@synthesize masterPopoverController = _masterPopoverController;
@synthesize contact = _contact;

@synthesize nameLabel;
@synthesize titleLabel;
@synthesize phoneLabel;
@synthesize emailLabel;
@synthesize twitterIdLabel;


#pragma mark - Managing the detail item

- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;
        
        // Update the view.
        [self configureView];
    }

    if (self.masterPopoverController != nil) {
        [self.masterPopoverController dismissPopoverAnimated:YES];
    }        
}

- (void)configureView
{
    // Update the user interface for the detail item.

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    [self configureView];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (_contact) {        
        //setting values from Object
        self.nameLabel.text = _contact.name;
        self.titleLabel.text = _contact.title;
        self.phoneLabel.text = _contact.phone;
        self.emailLabel.text = _contact.email;
        self.twitterIdLabel.text = _contact.twitterId;
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

#pragma mark - Split view

- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
    barButtonItem.title = NSLocalizedString(@"Master", @"Master");
    [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
    self.masterPopoverController = popoverController;
}

- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    // Called when the view is shown again in the split view, invalidating the button and popover controller.
    [self.navigationItem setLeftBarButtonItem:nil animated:YES];
    self.masterPopoverController = nil;
}

#pragma mark - Table View Data Source

-(IBAction)onEditContact:(id)sender {
    if (self.contactActionViewController == nil) {
        self.contactActionViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ContactAction"];
    }
    
    self.contactActionViewController.contact = self.contact;
    self.contactActionViewController.action = EDIT;
    
    [self.navigationController pushViewController:self.contactActionViewController animated:YES];
}

-(IBAction)onDeleteContact:(id)sender
{
    NSString* formattedContactText = [NSString stringWithFormat:@"%@", self.contact.name];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:CONFIRMATION_POPUP_TEXT message:formattedContactText delegate:self cancelButtonTitle:CANCEL_TEXT otherButtonTitles:CONFIRM_TEXT, nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    //only need to act on the confirm button
    if (CONFIRM_BUTTON_INDEX == buttonIndex) {
        //delete the contact and return to main view
        [[ContactList singleton] deleteContact:self.contact];
        [self.navigationController popViewControllerAnimated:YES];
    }
}


@end
